/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.action.changemapping;

import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bff.api.dataprocessor.IChangeConvertor;
import com.inspur.edp.bff.api.manager.context.ChangeMapperContext;
import com.inspur.edp.bff.spi.utils.BffActionNames;
import com.inspur.edp.cef.entity.changeset.IChangeDetail;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import lombok.var;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class DefaultChangeMapAction extends ChangeMappingAction {

    public DefaultChangeMapAction(ChangeMapperContext context) {
        super(context);
    }

    @Override
    protected void execute() {
        if (getMapContext().getInnerChange() == null || getMapContext().getInnerChange().size() < 1) {
            return;
        }
        java.util.ArrayList<IChangeDetail> defaultChanges = null;
        if (getMapContext().getInnerChange().containsKey(BffActionNames.DefaultName)) {
            defaultChanges = getMapContext().getInnerChange().get(BffActionNames.DefaultName);
        } else {
            for (ArrayList<IChangeDetail> changes : getMapContext().getInnerChange().values()) {
                defaultChanges = changes;
                break;
            }
        }

        //TODO 临时处理

        HashMap<String, IStandardLcp> lcps = getContext().getLcps();
        HashSet<String> dataIds = new HashSet<>();
        if (lcps != null && lcps.size() > 0) {

            for (IStandardLcp lcp : lcps.values()) {
                dataIds.addAll(lcp.getDataIds());
            }
        }

        if (defaultChanges == null) {
            return;
        }

        IChangeConvertor changeConvertor = getContext().getChangeConvertor();

        for (IChangeDetail defaultChange : defaultChanges) {
            if (dataIds.size() > 0 && dataIds.contains(defaultChange.getDataID())) {

                IChangeDetail voChange = changeConvertor.convertFromBEChange(defaultChange);
                getMapContext().getTargetChange().add(voChange);

                dealExtendMapping(voChange, defaultChange);
            }
        }
    }

    private void dealExtendMapping(IChangeDetail voChange, IChangeDetail beChange) {
        var extList = getContext().getExtList();
        if (extList == null || extList.size() < 1)
            return;
        for (var ext : extList) {
            ext.mapChange2Vo((ModifyChangeDetail) voChange, (ModifyChangeDetail) beChange);
        }
    }
}
